package views;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.example.zx.theline.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import Utils.DictionariesUtil;
import app.MyApplication;
import bean.CheckSearchBean;

import com.example.zx.theline.sgjh.CheckListApFragment;
import greendao.UnitInformationDao;
import greendao.entity.UnitInformation;

/**
 * Created by Ren on 2017/8/4.
 */

public class CheckSearchView extends RelativeLayout {


    //线名
    EditText xm;
    //开始日期
    TextView sgrq;
    //计划号
    EditText rjhh;
    //检查人
    EditText jcr;
    //单位和部门
    EditText dwhbm;

    RadioGroup sfcs;
    RadioButton cs, wcs;


    ListPopWindow dwhbmPop;

    //确定
    TextView confirm;
    //取消
    TextView cancel;
    //清空
    TextView delect;

    OnClick mOnClick;
    CheckSearchBean mCheckSearchBean;
    private List<UnitInformation> mAutoSource = new ArrayList<>();

    List<UnitInformation> mSelectUnitList = new ArrayList<>();

    public CheckSearchView(Context context) {
        super(context);
        initView();
        initAutoCompleteTextView();
        initListener();
    }

    public CheckSearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        initAutoCompleteTextView();
        initListener();
    }

    public CheckSearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initAutoCompleteTextView();
        initListener();

    }

    private void initView() {

        LayoutInflater.from(getContext()).inflate(R.layout.checksearchview, this);


        xm = (EditText) findViewById(R.id.xm);
        xm.clearFocus();
        sgrq = (TextView) findViewById(R.id.sgrq);
        rjhh = (EditText) findViewById(R.id.rjhh);
        rjhh.clearFocus();
        jcr = (EditText) findViewById(R.id.jcr);
        jcr.clearFocus();
        dwhbm = (EditText) findViewById(R.id.dwhbm);
        sfcs = (RadioGroup) findViewById(R.id.sfcs);
        cs = (RadioButton) findViewById(R.id.cs);
        wcs = (RadioButton) findViewById(R.id.wcs);


        confirm = (TextView) findViewById(R.id.confirm);
        cancel = (TextView) findViewById(R.id.cancel);
        delect = (TextView) findViewById(R.id.delect);

        if (dwhbmPop == null) {
            dwhbmPop = new ListPopWindow.Builder(getContext())
                    .setList(DictionariesUtil.getUnitList())
                    .setOutsideTouchable(false)
                    .setParentView(dwhbm)
                    .setOnListItemCilck(new ListPopWindow.OnListItemCilck() {
                        @Override
                        public void OnListItemCilck(int position) {
                            dwhbm.setText(DictionariesUtil.getUnitList().get(position).toString());
                            dwhbmPop.dismiss();
                        }
                    }).builder();
        }

    }


    private void initListener() {


        //确定
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCheckSearchBean.setXm(xm.getText().toString());
                mCheckSearchBean.setRjhh(rjhh.getText().toString());
                mCheckSearchBean.setJcr(jcr.getText().toString());
                if(wcs.isChecked()){
                    mCheckSearchBean.setSfcs(CheckListApFragment.NOTTIMEOUT);
                } else {
                    mCheckSearchBean.setSfcs(CheckListApFragment.TIMEOUT);
                }
                if (dwhbm.getText().toString().length() > 0) {
                    mSelectUnitList = MyApplication.getDaoSession().getUnitInformationDao().queryBuilder()
                            .where(UnitInformationDao.Properties.LjyyxDwxxName.eq(dwhbm.getText().toString())).list();
                    if (mSelectUnitList.size() == 0) {
                        mCheckSearchBean.setDwhbm(null);
                    } else {
                        mCheckSearchBean.setDwhbm(mSelectUnitList.get(0));
                    }
                }
                if (mOnClick != null) {
                    mOnClick.onConfirmListener(mCheckSearchBean);
                }
                CheckSearchView.this.setVisibility(GONE);
            }
        });
        //取消
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckSearchView.this.setVisibility(GONE);
            }
        });
        //清空
        delect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                xm.setText("");
                sgrq.setText("");
                rjhh.setText("");
                jcr.setText("");
                dwhbm.setText("");
                cs.setChecked(false);
                wcs.setChecked(true);
                mCheckSearchBean = new CheckSearchBean();
            }
        });
    }

    //初始化AutoCompleteTextView
    private void initAutoCompleteTextView() {


        dwhbm.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    return;
                } else {
                    if (!dwhbmPop.getWindow().isShowing()) {
                        dwhbmPop.show();
                    }
                    mAutoSource = MyApplication.getDaoSession()
                            .getUnitInformationDao()
                            .queryBuilder()
                            .where(UnitInformationDao.Properties.LjyyxDwxxName.like("%" + s + "%")).list();
                    if (mAutoSource.size() == 0) {
                        UnitInformation un = new UnitInformation();
                        un.setLjyyxDwxxName("没有匹配的单位");
                        mAutoSource.add(un);
                    }
                    dwhbmPop.setList(mAutoSource);
                }
            }
        });

    }




    public void setConfirmListener(OnClick onClick) {
        this.mOnClick = onClick;
    }

    public void setDefaultDate(CheckSearchBean bean) {
        if (mCheckSearchBean == null) {
            mCheckSearchBean = bean;
        }
    }

    public  void hideSfcs(){
        sfcs.setVisibility(GONE);
    }

    public interface OnClick {
        void onConfirmListener(CheckSearchBean Util);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return true;
    }


}
